package com.brotherly.framework.storage;

import java.io.InputStream;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.brotherly.framework.app.BaseApplication;
import com.brotherly.framework.service.IService;
import com.brotherly.geo.MapHelper;
import com.brotherly.util.Factory;
import com.brotherly.util.FileSystem;
import com.brotherly.util.ReusablePool;

public class SimpleStorageService implements Factory<SimpleStorage>, IService
{
    public static final String description = "com.brotherly.framework.storage.SIMPLE_STORAGE";
    public static final String prefix = "simple.storage.";

    protected Context context;
    protected ReusablePool<SimpleStorage> pools;

    public SimpleStorageService(Service service)
    {
        context = service;
        pools = new ReusablePool<SimpleStorage>(100, this);

        loadFromLocal();
    }

    public void deleteStorage(String name)
    {
        SimpleStorage storage = openStorage(name);

        storage.clear();
        storage.commit();

        storage.close();
    }

    @Override
    public void init()
    {

    }

    @Override
    public void init(SimpleStorage element)
    {}

    public void loadFromLocal()
    {
        String json_settings = null;
        JSONObject inbuilt_setting = null;

        try
        {
            InputStream input = BaseApplication.getInstance().getAssets().open("data/settings.json");

            json_settings = FileSystem.readString(input, "utf-8");

            input.close();

            inbuilt_setting = (JSONObject) (new JSONParser()).parse(json_settings);
        }
        catch (Exception e)
        {
            Log.i(description, String.format("can't load default settings from assets %s", e.toString()));
        }

        SimpleStorageService storageServ = this;

        for (Object key_item : inbuilt_setting.keySet())
        {
            String key = key_item.toString();

            JSONObject values = (JSONObject) inbuilt_setting.get(key);
            try
            {
                SimpleStorage storage = storageServ.openStorage(key);

                for (Object name_item : values.keySet())
                {
                    String name = name_item.toString();

                    String value = MapHelper.getString(values, name);

                    String existed_value = storage.get(name);
                    if (existed_value == null)
                    {
                        storage.put(name, value);
                    }
                }

                storage.commit();
                storage.close();
            }
            catch (Exception e)
            {
                Log.i(description, String.format("can't save default settings %s to storage %s", key, e.toString()));
            }
        }
    }

    @Override
    public SimpleStorage newInstance()
    {
        return new SimpleStorage();
    }

    public SimpleStorage openStorage(String name)
    {
        SharedPreferences preferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        if (preferences != null)
        {
            SimpleStorage storage = pools.alloc();
            storage.setSharedPreferences(preferences, pools);

            return storage;
        }

        return null;
    }

    @Override
    public void uninit(SimpleStorage element)
    {}
}
